/*
 * @Author: 刘美红 liumeihong@llootong.com
 * @Date: 2025-04-07 09:55:59
 * @LastEditors: 刘美红 liumeihong@llootong.com
 * @LastEditTime: 2025-04-17 10:48:00
 * @FilePath: \gzt-template\src\pinia\tabbar.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia';
import { ref } from 'vue';
export const TabbarList = defineStore(
  'TabbarList',
  () => {
    const tabbarList = ref([]);
    const currentMenu = ref({ path: '' });
    function add(item) {
      console.log('add执行');
      let index = tabbarList.value.findIndex((current) => {
        return item.path === current.path;
      });

      if (index > -1) {
        // 去切换路由
        currentMenu.value = item;
      } else {
        if (tabbarList.value.length == 20) {
          tabbarList.value.pop();
        } else {
          tabbarList.value.push(item);
        }

        currentMenu.value = item;
      }
    }

    function deleted(val) {
      if (tabbarList.value.length === 1) {
        return;
      }
      let index = tabbarList.value.findIndex((item) => item.path == val.path);
      if (index > -1) {
        tabbarList.value.splice(index, 1);
        currentMenu.value = tabbarList.value.at(-1);
      }
    }

    // 关闭全部:只保留工作台
    function clearAll(type) {
      if (type) {
        tabbarList.value = tabbarList.value.slice(0, 1);
        currentMenu.value = tabbarList.value[0];
      } else {
        tabbarList.value = [];
      }
    }

    function setCurrent(val) {
      currentMenu.value = val;
    }

    return {
      setCurrent,
      currentMenu,
      tabbarList,
      add,
      deleted,
      clearAll,
    };
  },
  {
    persist: false, //开启持久化存储
  }
);
